package com.rzhd.coursepatriarch.common.view;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * @author huling
 * @date: 2019/4/4.
 */
public class RelativeLayoutView extends RelativeLayout {

    public static final int KEYBORAD_HIDE = 0;
    public static final int KEYBORAD_SHOW = 1;
    private static final int SOFTKEYPAD_MIN_HEIGHT = 50;
    private KeyBordStateListener  keyBordStateListener;
    private Handler uiHandler = new Handler();

    public RelativeLayoutView(Context context) {
        super(context);
    }

    public RelativeLayoutView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RelativeLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RelativeLayoutView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onSizeChanged(int w,final int h, int oldw,final int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                if (oldh - h > SOFTKEYPAD_MIN_HEIGHT){
                    keyBordStateListener.stateChange(KEYBORAD_SHOW);
                }else {
                    if(keyBordStateListener != null){
                        keyBordStateListener.stateChange(KEYBORAD_HIDE);}
                }
            }
        });
    }

    public void setKeyBordStateListener(KeyBordStateListener keyBordStateListener) {
        this.keyBordStateListener = keyBordStateListener;
    }
    public interface KeyBordStateListener{
        void stateChange(int state);
    }
}